// pages/book-list/index.js
let p = 0  //全局变量，会被小程序全局缓存
let id
Page({

    /**
     * 页面的初始数据
     */
    data: {
        booklist:[]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(options);
        wx.setNavigationBarTitle({
          title: options.tit,
        })
        p=0
        id = options.id || '62c69d8c5ee85713b0b7765d'
        this.fetchData()
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        console.log('触底了');
        this.fetchData()
    },
    onPullDownRefresh(){ //下拉刷新
        console.log('下拉刷新');
        p=0
        this.setData({booklist:[]})
        this.fetchData()
    },
    fetchData(){ //数据加载方法封装
        let url = `/1.1/classes/Books?where={"cateId":"${id}"}&limit=12&skip=${p*12}`
        getApp().fly.get(url).then(res=>{
            wx.stopPullDownRefresh() //关闭下拉刷新动画
            this.setData({
                booklist:[
                    ...this.data.booklist,
                    ...res.data.results
                ]
            })
            p++
        })
    },
})